package com.jhhe.mr.mysort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class MySortBean implements WritableComparable<MySortBean> {
    private Long id;
    private Long num;

    public MySortBean() {
    }

    public MySortBean(Long id, Long num) {
        this.id = id;
        this.num = num;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        System.out.println("equals方法执行了。。。");
        return super.equals(obj);
    }

    @Override
    public String toString() {
        return id + "\t" + num;
    }

    @Override
    public int compareTo(MySortBean o) {
        System.out.println("compareTo 方法执行了。。。");
        //指定按照bean对象的总时长字段的值进行比较
        // 返回值三种：0：相等, 1：小于, -1：大于
        if (this.num <  o.num) {
            return -1;
        } else if (this.num > o.num) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(num);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.num = in.readLong();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getNum() {
        return num;
    }

    public void setNum(Long num) {
        this.num = num;
    }
}
